import React from 'react';
import routes from '../routes';
import PropTypes from 'prop-types';
import Export from '../../common/Export';
import {PERMISSION} from '../constants';
import {FORM_OPTIONS} from '../../constants';
import {Button, Form, Row, Col, Card} from 'antd';
import {ListTypeCL, hasPermissions} from '../utils';
import TextInput from 'Shared/components/TextInput';
import WrappedSelect from '../../common/WrappedSelect';
import {injectIntl, language, formatMessage} from '../intl';
import {isYesNo, technologyActivityStatus} from '../../Enum';
import DateRangePicker from 'Shared/components/DateRangePicker';
import DealerSelectPanel from '../../common/DealerSelect/SelectEdit';
const FormItem = Form.Item;
const isYesNoList = isYesNo.toList(language);
const technologyActivityStatusList = technologyActivityStatus.toList(language);
class QueryPanel extends React.PureComponent {
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }
    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }
    onSelectDealer = data => {
        this.props.onConditionsChange('dealerId', data.id);
        this.props.onQueryDealerChange({
            id: data.id,
            code: data.code,
            name: data.name,
        });
    }
    render() {
        const {conditions, querySearch, onResetClick,
            onClickExport, loading, permissions, initEnumData,
            queryDealer} = this.props;
        const otherBtns = []; const operations = [];
        const dealerInfo = conditions.dealerId ? {
            id: queryDealer.id,
            code: queryDealer.code,
            name: queryDealer.name
        } : null;
        if(hasPermissions(permissions, PERMISSION.export))
            otherBtns.push(
                <Export key={PERMISSION.export} exportRequest={onClickExport}/>
            );
        if(hasPermissions(permissions, PERMISSION.add))
            operations.push(
                <Button key="add" type="primary" href={`#${routes.add.url()}`}>
                    {formatMessage({
                        id: 'queryPanel.btn.add',
                        defaultMessage: '新增'
                    })}
                </Button>
            );
        return (
            <div>
                <Form className="form-standard">
                    <Card>
                        <Row>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.code',
                                    defaultMessage: '编号'
                                })}{...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="code"
                                        value={conditions.code}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onChange={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={formatMessage({
                                        id: 'queryPanel.form.brandId',
                                        defaultMessage: '品牌'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <WrappedSelect
                                        allowClear
                                        name="brandId"
                                        value={conditions.brandId}
                                        options={initEnumData.brands}
                                        onChange={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            {!initEnumData.isDealerUser && <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={formatMessage({
                                        id: 'queryPanel.form.dealer',
                                        defaultMessage: '总代'
                                    })}
                                    {...FORM_OPTIONS.item} >
                                    <DealerSelectPanel
                                        value={dealerInfo}
                                        onSelect={this.onSelectDealer} />
                                </FormItem>
                            </Col>}
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.status',
                                    defaultMessage: '状态'
                                })}{...FORM_OPTIONS.item}>
                                    <WrappedSelect
                                        allowClear
                                        name="status"
                                        value={conditions.status}
                                        options={technologyActivityStatusList}
                                        onChange={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={formatMessage({
                                        id: 'queryPanel.form.isDealerApply',
                                        defaultMessage: '是否总代申请'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <WrappedSelect
                                        allowClear
                                        name="isDealerApply"
                                        value={conditions.isDealerApply}
                                        options={isYesNoList}
                                        onChange={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.serviceName',
                                    defaultMessage: '技术升级主题'
                                })}{...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="serviceName"
                                        value={conditions.serviceName}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onChange={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.technologyActivityDate',
                                    defaultMessage: '技术升级截止日期'
                                })}
                                {...FORM_OPTIONS.item}>
                                    <DateRangePicker
                                        name="technologyActivityDate"
                                        value={conditions.technologyActivityDate}
                                        onChange={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                        </Row>
                        <Row>
                            <Col span={6}>
                                <Button key="query" type="primary" onClick={querySearch} loading={loading}>
                                    {formatMessage({
                                        id: 'queryPanel.btn.query',
                                        defaultMessage: '查询'
                                    })}</Button>
                                <Button key="clear" onClick={onResetClick}>
                                    {formatMessage({
                                        id: 'queryPanel.btn.reset',
                                        defaultMessage: '重置'
                                    })}</Button>
                            </Col>
                            <Col span={18} className="col-align-right">
                                {otherBtns}
                                {operations}
                            </Col>
                        </Row>
                    </Card>
                </Form>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    initEnumData: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    onClickExport: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onQueryDealerChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    queryDealer: PropTypes.object,
};
import {connect} from 'react-redux';
import {querySearch, resetQueryPanel, saveQueryCondition, saveQueryDealer, exportData} from '../actions';
import {createSelector} from 'reselect';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getinitEnumData = createSelector(
    state => state.getIn(['page', 'domainData', 'init']),
    init => {
        const initData = init.toJS();
        const isDealerUser = initData.isDealerUser;
        const brands = initData.brands ? ListTypeCL(initData.brands) : [];
        return {
            brands, // 品牌
            initData,
            isDealerUser
        };
    }
);

const getQueryDealer = selectorFactory(['page', 'appState', 'queryDealer']);
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = state => ({
    conditions: getConditions(state),
    initEnumData: getinitEnumData(state),
    queryDealer: getQueryDealer(state),
    permissions: getPermissions(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching'])
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    querySearch: () => dispatch(querySearch()),
    onClickExport: () => dispatch(exportData()),
    onResetClick: () => dispatch(resetQueryPanel()),
    onQueryDealerChange: data => dispatch(saveQueryDealer(data)),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
